package com.sanjiahantang.youjiaclient.module.common.views.impl;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiaclient.R;
import com.sanjiahantang.youjiaclient.base.BaseMvpActivity;
import com.sanjiahantang.youjiaclient.base.CallBack;
import com.sanjiahantang.youjiaclient.config.ConfigValue;
import com.sanjiahantang.youjiaclient.config.SPConfig;
import com.sanjiahantang.youjiaclient.http.BaseDelegate;
import com.sanjiahantang.youjiaclient.http.OkHttpClientManager;
import com.sanjiahantang.youjiaclient.module.common.beans.MessageBean;
import com.sanjiahantang.youjiaclient.module.common.beans.MessageUseNoticeBean;
import com.sanjiahantang.youjiaclient.module.common.models.IUserModel;
import com.sanjiahantang.youjiaclient.module.common.models.impl.UserModel;
import com.sanjiahantang.youjiaclient.module.common.presenters.MessagePresenter;
import com.sanjiahantang.youjiaclient.module.common.views.MessageView;
import com.sanjiahantang.youjiaclient.module.goods.views.impl.GoodsDetailActivity;
import com.sanjiahantang.youjiaclient.module.match.views.impl.MatchActivity;
import com.sanjiahantang.youjiaclient.module.me.require.beans.RequireListBean;
import com.sanjiahantang.youjiaclient.module.me.require.views.impl.RequireDetailActivity;
import com.sanjiahantang.youjiaclient.module.me.scheme.views.impl.MeSchemeDetailActivity;
import com.sanjiahantang.youjiaclient.utils.SPUtils;
import com.squareup.okhttp.Request;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_NOTICE_CONTENT;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_TYPE;
import static com.sanjiahantang.youjiaclient.config.StaticValue.WEB_TYPE_MESSAGE;


/**
 * Created by sks on 2015/9/23.
 * 我的消息
 */
public class MessageActivity extends BaseMvpActivity<MessageView, MessagePresenter> implements View.OnClickListener, MessageView,
        AdapterView.OnItemClickListener {
    private final int INTENT_REQUEST_CODE = 200;
    private Context context;
    private TextView title;
    private ImageView back;
    private ListView listView;
    private IUserModel userModel;
    private int index;
    private ArrayList<MessageBean.Data> list = new ArrayList<>();

    @Override
    public MessagePresenter initPresenter() {
        userModel = new UserModel();
        return new MessagePresenter();
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_message);
        context = this;
        title = (TextView) findViewById(R.id.title);
        back = (ImageView) findViewById(R.id.back);
        back.setBackgroundResource(R.drawable.i_back_black);
        title.setText("通知中心");
        findViewById(R.id.fl_Left).setOnClickListener(this);
        listView = (ListView) findViewById(R.id.listView);


        listView.setAdapter(presenter.getAdapter(context));
        listView.setOnItemClickListener(this);
    }

    @Override
    public void initData() {
        presenter.getMessage(context);
    }


    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Log.e("item", i + "//");
        index = i;
        final MessageBean.Data item = presenter.getDataList().get(index);
        if (item == null) {
            Utils.showToast(context, "无效通知");
            return;
        }

        if ("1".equals(item.getReadFlg())) {
            //已读消息直接进入WebActivity
            if ("0".equals(item.getPushType())) {
                Intent intent = new Intent(context, WebActivity.class);
                intent.putExtra(KEY_TYPE, WEB_TYPE_MESSAGE);
                intent.putExtra(KEY_NOTICE_CONTENT, presenter.getDataList().get(index).getNoticeContent());
                startActivityForResult(intent, INTENT_REQUEST_CODE);
            } else if ("13".equals(item.getPushType())) {
                Intent intent = new Intent(context, WebActivity.class);
                intent.putExtra(KEY_TYPE, WEB_TYPE_RICHTEXT);
                intent.putExtra(KEY_NOTICE_CONTENT, presenter.getDataList().get(index).getContent());
                intent.putExtra(KEY_TITLE, presenter.getDataList().get(index).getNoticeTitle());
                startActivityForResult(intent, INTENT_REQUEST_CODE);
            } else {
                if (item.getPushDestId() == null) {
                    Utils.showToast(context, "无效通知");
                    return;
                }
                intentPage(item.getPushType(), item.getPushDestId());
            }
        } else {
            //未读消息访问已读未读接口后进入WebActivity
            Map<String, Object> params = new HashMap<>();
            params.put("snId", item.getSnId());
            params.put("customerPhone", SPUtils.get(context, SPConfig.PHONE, "").toString());
            params.put(KEY_TOKEN, ConfigValue.DATA_KEY);
            userModel.getUseNoticeForCustomer(context, params, new CallBack<MessageUseNoticeBean>() {
                @Override
                public void onSuccess(MessageUseNoticeBean response, Context context, Object tag) {
                    if (response != null && response.getCode() == RESPONSE_CODE_SUC) {
                        Log.e("返回", "" + response.getData());
                        initData();
                        if ("0".equals(item.getPushType())) {
                            Intent intent = new Intent(context, WebActivity.class);
                            intent.putExtra(KEY_TYPE, WEB_TYPE_MESSAGE);
                            intent.putExtra(KEY_NOTICE_CONTENT, presenter.getDataList().get(index).getNoticeContent());
                            startActivityForResult(intent, INTENT_REQUEST_CODE);
                        } else if ("13".equals(item.getPushType())) {
                            Intent intent = new Intent(context, WebActivity.class);
                            intent.putExtra(KEY_TYPE, WEB_TYPE_RICHTEXT);
                            intent.putExtra(KEY_NOTICE_CONTENT, presenter.getDataList().get(index).getContent());
                            intent.putExtra(KEY_TITLE, presenter.getDataList().get(index).getNoticeTitle());
                            startActivityForResult(intent, INTENT_REQUEST_CODE);
                        } else {
                            if (item.getPushDestId() == null) {
                                Utils.showToast(context, "无效通知");
                                return;
                            }
                            intentPage(item.getPushType(), item.getPushDestId());
                        }

                    } else {
                        Utils.showToast(context, "无效通知");
                    }

                }

                @Override
                public void onFailure(String message) {
                    Utils.showToast(context, "无效通知");
                }
            });
        }
    }

    private void intentPage(String type, String destId) {
        if ("1".equals(type) || "5".equals(type) || "10".equals(type)) {
            //获取表单详情页面数据
//            Map<String,String> params=new HashMap<>();
//            String token= SPUtils.get(context, SPConfig.KEY,"").toString();
            String pid = destId;
//            Log.e("参数信息",token+"///"+pid);
//            params.put("token",token);
//            params.put("requireId",pid);
//            getRequireDetail(context,params);

            Intent i = new Intent(context, RequireDetailActivity.class);
            i.putExtra(KEY_ID, Integer.valueOf(pid));
            i.putExtra(KEY_JPUSH_RECEIVER, INTENT_JPUSH_RECEIVER);
            //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            context.startActivity(i);
        } else if ("2".equals(type)) {
            String pid = destId;
            //打开方案页面
            Intent i = new Intent(context, MeSchemeDetailActivity.class);
            i.putExtra(KEY_ID, destId);
            i.putExtra(KEY_JPUSH_RECEIVER, INTENT_JPUSH_RECEIVER);
            //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            context.startActivity(i);
        } else if ("3".equals(type)) {
            //获取表单详情页面数据
            String pid = destId;
            Intent i = new Intent(context, GoodsDetailActivity.class);
            i.putExtra("goodsId", Integer.valueOf(pid));
            i.putExtra(KEY_JPUSH_RECEIVER, INTENT_JPUSH_RECEIVER);
            //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            context.startActivity(i);
        } else if ("4".equals(type)) {
            //获取表单详情页面数据
            String pid = destId;
            Intent i = new Intent(context, MatchActivity.class);
            i.putExtra(KEY_ID, Integer.valueOf(pid));
            i.putExtra(KEY_JPUSH_RECEIVER, INTENT_JPUSH_RECEIVER);
            //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            context.startActivity(i);
        } else if ("5".equals(type)) {
            int pid = Integer.valueOf(destId);
            presenter.goOrderConfirmPage(context, pid);
        } else if ("6".equals(type)) {
            int pid = Integer.valueOf(destId);
            //打开首页
            Intent i = new Intent(context, MainActivity.class);
            i.putExtra(KEY_PAGE, 1);
            ConfigValue.param = pid;
            //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            context.startActivity(i);
        } else if ("0".equals(type)) {
            Intent intent1 = new Intent(context, WebActivity.class);
            intent1.putExtra("url", destId);
            context.startActivity(intent1);
        } else if ("13".equals(type)) {
            presenter.findNoticeInfoById(context, destId, new OnLoadListener() {

                @Override
                public void onLoad(Object content) {

                }
            });
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == INTENT_REQUEST_CODE) {
            if (!"1".equals(presenter.getDataList().get(index).getReadFlg())) {
                //点击未读消息进入WebActivity后，返回重新刷新消息列表；反之，不刷新
                presenter.getMessage(context);
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fl_Left:
                finish();
                break;
        }
    }

    @Override
    public void showProgress() {
        super.showProgress($this);
    }

    /**
     * 获取表单详情信息
     *
     * @param context
     * @param params
     */
    private void getRequireDetail(final Context context, Map<String, String> params) {
        OkHttpClientManager.postAsyn(context, ConfigValue.API_URL + "/user/phone/require/findUserRequireByIdPhone",
                params, new BaseDelegate.ResultCallback<RequireListBean>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        Log.e("error", "error");
                        if (context == null) return;
                        Utils.showToast(context, "无效通知");
                    }

                    @Override
                    public void onResponse(RequireListBean response, Object tag) {
                        if (context == null) return;
                        if (response != null && RESPONSE_CODE_SUC == response.getCode()) {
                            //打开表单详情页面
                            Intent i = new Intent(context, RequireDetailActivity.class);
                            i.putExtra(KEY_PARAM, response.getItem());
                            i.putExtra(KEY_JPUSH_RECEIVER, INTENT_JPUSH_RECEIVER);
                            //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            context.startActivity(i);
                        } else {
                            Utils.showToast(context, "无效通知");
                        }
                    }
                });
    }
}
